/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ssn.zone.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import ssn.common.code.vo.CodeVO;
import ssn.common.util.Util;
import ssn.common.web.CommonController;
import ssn.zone.service.ZoneService;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import egovframework.rte.fdl.string.EgovStringUtil;


/**  
 * @Class Name : 
 * @Description : 
 * @Modification Information  
 * @
 * @  수정일      수정자              수정내용
 * @ ---------   ---------   -------------------------------
 * @ 2009.03.16           최초생성
 * 
 * @since 2013. 09.30
 * @version 1.0
 * @see
 */
@SuppressWarnings("rawtypes")
@Controller
public class ZoneController extends CommonController{
	
    @Resource(name = "zoneService")
    private ZoneService zoneService;
    
    /**
	 * 글 목록을 조회한다. 
	 * @param 
	 * @param model
	 * @return 
	 * @exception Exception
	 */
    @RequestMapping(value="/zone/zoneList.do")
    public String zoneList(@ModelAttribute("codeVO") CodeVO codeVO,
    			ModelMap model) throws Exception {
    	
    	if(EgovStringUtil.isEmpty(codeVO.getScc_ref_key())){
    		codeVO.setScc_ref_key(propertiesService.getString("scc.codekey.zone"));
    	}
    	
    	/** pageUnit : 페이지 리스트 개수, pageSize : 페이징 개수
    	codeVO.setPageUnit(15);
    	codeVO.setPageSize(15);
    	*/
        
    	Map resultMap = zoneService.selectZoneList(codeVO);
    	List resultList = (List)resultMap.get("resultList");
    	codeVO.setTotalRecordCount((Integer)resultMap.get("resultListCnt"));
    	model.addAttribute("resultList", resultList);
    	model.addAttribute("codeVO", codeVO);
        
        return "/zone/zoneList";
    }
    
    /**
     * 뷰페이지
     * @param codeVO
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/zone/zoneView.do")
    public String zoneView(@ModelAttribute("codeVO") CodeVO codeVO,
    			ModelMap model) throws Exception {
    	
    	if(EgovStringUtil.isEmpty(codeVO.getScc_code_key())){
    		throw new EgovBizException(propertiesService.getString("info.nodata.msg"));
    	}
    	CodeVO resultVO = zoneService.selectZoneInfo(codeVO);
    	if(resultVO != null){
    		resultVO.setSearchParam(codeVO);
    	}
    	
    	model.addAttribute("resultVO", resultVO);
        return "/zone/zoneView";
    }
    
    /**
     * 글쓰기 수정폼
     * @param codeVO
     * @param model
     * @return
     * @throws Exception
     */
    
    @RequestMapping(value="/zone/zoneWriteForm.do")
    public String zoneWriteForm(@ModelAttribute("codeVO") CodeVO codeVO,
    			ModelMap model) throws Exception {
    	if(EgovStringUtil.isNotEmpty(codeVO.getScc_code_key())){
    		CodeVO resultVO = zoneService.selectZoneInfo(codeVO);
    		if(resultVO != null){
        		resultVO.setSearchParam(codeVO);
        	}
    		model.addAttribute("resultVO", resultVO);
    	}else{
    		model.addAttribute("resultVO", codeVO);
    	}
        return "/zone/zoneWriteForm";
    }
    
    @RequestMapping(value="/zone/zoneSave.do", method=RequestMethod.GET)
    public String zoneWriteFormGet(ModelMap model) throws Exception {
    	return "redirect:/zone/zoneWriteForm.do";
    }
    /**
     * 저장/수정
     * @param codeVO
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/zone/zoneSave.do", method=RequestMethod.POST)
    public ModelAndView zoneSave(@ModelAttribute("codeVO") @Valid CodeVO codeVO, BindingResult bindingResult) throws Exception {
    	ModelAndView mav = new ModelAndView();
    	mav.setView(new RedirectView(servletContext.getContextPath()+"/zone/zoneView.do"));
    	
    	if (bindingResult.hasErrors()) {
    		for(FieldError error :  bindingResult.getFieldErrors()){
    			mav.addObject("error_"+error.getField(), error.getDefaultMessage());
    		}
    		mav.setViewName("/zone/zoneWriteForm");
    		mav.addObject("resultVO", codeVO);
			return mav;
    	}
    	if(EgovStringUtil.isNotEmpty(codeVO.getScc_code_key())){
    		zoneService.updateZoneInfo(codeVO);
    	}else{
    		/*UUID uuid = UUID.randomUUID();
    		codeVO.setScc_code_key(uuid.toString());*/
    		codeVO.setScc_ref_key(propertiesService.getString("scc.codekey.zone"));
    		zoneService.insertZoneInfo(codeVO);
    	}
    	mav.addObject("scc_code_key", codeVO.getScc_code_key());
    	mav.addObject("pageIndex", codeVO.getPageIndex());
    	return mav;
    } 
}
